package com.xu.cloud.shorturl.factory;

import com.xu.cloud.common.sequence.ISequence;
import com.xu.cloud.common.util.LocalDateTimeUtil;
import com.xu.cloud.common.util.NumberConvertUtil;
import com.xu.cloud.shorturl.pojo.domain.ShortUrlInfoDO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * ShortUrlInfoDO简单工厂实现类, 通过序列转62进制生成短地址
 *
 * @author xuguan
 * @since 2024/3/9
 */
@Component
@RequiredArgsConstructor
public class SequenceShortUrlInfoDOFactory implements ShortUrlInfoDOFactory {
    private final ISequence sequence;

    @Override
    public ShortUrlInfoDO create(String originUrl) {
        final long id = Long.parseLong(sequence.nextId());
        final String shortUrl = NumberConvertUtil.numToBase(id, NumberConvertUtil.BASE_62);

        final ShortUrlInfoDO shortUrlInfoDO = new ShortUrlInfoDO();
        shortUrlInfoDO.setId(id);
        shortUrlInfoDO.setShortUrl(shortUrl);
        shortUrlInfoDO.setOriginUrl(originUrl);
        shortUrlInfoDO.setCreateTime(LocalDateTimeUtil.currentDateTime());

        return shortUrlInfoDO;
    }
}
